home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17768 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  67 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: 76623,2065@compuserve.com  (Bobby Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Virtualizing/Deriving
  5. Date: 17 Apr 1996 13:42:14 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4l2sfm$ln6@dub-news-svc-2.compuserve.com>
  8. References: <40.91023.1613@channel1.com> <4knlec$lno@qualcomm.com>
  9. Reply-To: 76623,2065@compuserve.com (Bobby Martin)
  10. NNTP-Posting-Host: dd21-037.compuserve.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. In <4knlec$lno@qualcomm.com>, drew@qualcomm.com (Drew Eckhardt) writes:
  14. >In article <40.91023.1613@channel1.com>,
  15. >Dspse Bedford <dspse.bedford@channel1.com> wrote:
  16. >>I just started using C++ so bare with me.
  17. >>
  18. >>Lets say I have a base class, BASE, with lots and lots of operators and 
  19. >>member functions virtualized, like 10,000. Now I create 100 derived classes 
  20. >>with BASE as their parent.  The first 99 derived classes use all of the 
  21. >>operators and member function which exist in BASE, of course, if they need 
  22. >>to redefine any they will.  The last derived class does not use some of the 
  23. >>operators defined, lets say 2 out of 10,000. Now if I don't define these 2 
  24. >>operators, the compiler will go pick up the parent definition if a user of 
  25. >>the last derived class attempts to use any of these two operators.
  26. >>  What can I do such that the compiler will give an error to the user?  I 
  27. >>do not want to create another base class if possible.
  28. >
  29. >You can declare the functions you don't want called as private, and fail 
  30. >to define them.  Ie, given
  31. >
  32. >   class foo : public base {
  33. >   public:
  34. >      // stuff
  35. >
  36. >   private:
  37. >      void dont_call ();
  38. >   };
  39. >
  40. >a user will get an error message if he attempts to call dont_call 
  41. >(scope is resolved before overloading, so this will keep the user 
  42. >from getting at a dont_call function with _any_ type signature in 
  43. >the base class).
  44. >
  45. >-- 
  46. ><a href="http://www.poohsticks.org/drew/">Home Page</a>
  47. >Four boxes : soap, ballot, jury, ammo.  | Work: drew@Qualcomm.COM       
  48. >Use in that order.                      | Play: drew@PoohSticks.ORG    
  49.  
  50.     I wouldn't try this, if you have declared the methods as virtual then you
  51. must be calling them from a pointer to the base class.  Any code compiled with
  52. a pointer to the base class will know nothing about the method being redeclared
  53. private.  I believe the lack of a definition of a virtual function should cause
  54. a link error if that virtual function is ever used, *even if only from a base
  55. class pointer*.
  56.     If you need some methods to not be available to some children, you
  57. should solve it as the design problem it is, not as a coding problem.  Leave
  58. those methods out of the class BASE, and add a subclass of BASE that defines
  59. those methods (maybe called BaseAddOn).  Then inherit any of the children that
  60. need those methods from BaseAddOn and inherit the other children from BASE.
  61. Any function that needs only Base's functionality should use a BASE* to receive
  62. objects, and any function that also needs the extra methods should use a 
  63. BaseAddOn* to receive objects.
  64.  
  65. Hope that helps,
  66. Bobby Martin
  67.